package fylder.recycler.demo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.ButterKnife;
import fylder.recycler.demo.R;
import fylder.recycler.demo.tools.AnimatorUtils;

/**
 * 作为抽象RecyclerAdapter的基本配置
 * <p/>
 * Created by fylder on 2015/11/23.
 */
public abstract class BaseRecyclerAdapter extends RecyclerView.Adapter {

    private static final int TYPE_FOOTER_VIEW = 2;
    private int extraCount = 1;//额外多出来的

    final int STATS_EMPTY = 1;      //  空白
    final int STATS_LOADING = 2;    //  加载
    final int STATS_END = 3;        //  到最后一条

    Context context;

    protected int STATS = STATS_EMPTY;

    public BaseRecyclerAdapter(Context context) {
        this.context = context;
    }

//    public boolean hasFooterView() {
//        return footerView != null;
//    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        switch (viewType) {
            case TYPE_FOOTER_VIEW:
                View footerView = LayoutInflater.from(context).inflate(R.layout.loading_lay, parent, false);
                return new FooterViewHolder(footerView);
            default:
                return createExcludeViewHolder(parent, viewType);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if (getItemViewType(position) == TYPE_FOOTER_VIEW) {
            FooterViewHolder footerViewHodler = (FooterViewHolder) holder;

            if (STATS == STATS_LOADING) {
                footerViewHodler.lT.setVisibility(View.GONE);
                footerViewHodler.loading.setVisibility(View.VISIBLE);
                footerViewHodler.lay.setVisibility(View.VISIBLE);
//                AnimationDrawable animationDrawable = (AnimationDrawable) footerViewHodler.loading.getDrawable();
//                animationDrawable.start(); //不太理想,有八格格
                footerViewHodler.lT.setText("正在加载");
                AnimatorUtils.show(footerViewHodler.lay);
            } else if (STATS == STATS_END) {
                footerViewHodler.lT.setVisibility(View.VISIBLE);
                footerViewHodler.loading.setVisibility(View.GONE);
                footerViewHodler.lay.setVisibility(View.VISIBLE);
                footerViewHodler.lT.setText(R.string.load_end);
                AnimatorUtils.show(footerViewHodler.lay);
            } else {
                footerViewHodler.lay.setVisibility(View.GONE);
            }
        } else {
            onBindView(holder, position);
        }
    }

    /**
     * 获取该type的ViewHolder
     *
     * @param viewType
     * @return
     */
    abstract RecyclerView.ViewHolder createExcludeViewHolder(ViewGroup parent, int viewType);

    /**
     * 绑定数据
     *
     * @param holder
     * @param position
     */
    abstract void onBindView(RecyclerView.ViewHolder holder, int position);


    /**
     * （不包括headerView和footerView）
     *
     * @return 获取item的数量
     */
    public abstract int getItemCountExcludeView();

    @Override
    public int getItemCount() {
        int c = getItemCountExcludeView() + extraCount;
        // Log.w("123", c + "");
        return c;
    }

    /**
     * 通过realItemPosition得到该item的类型（不包括headerView和footerView）
     *
     * @param realItemPosition 位置
     * @return 得到该item的类型
     */
    public abstract int getItemViewTypeExcludeExtraView(int realItemPosition);

    @Override
    public int getItemViewType(int innerPosition) {
        if (getItemCount() - 1 == innerPosition) { // footer
            return TYPE_FOOTER_VIEW;
        } else {
            return getItemViewTypeExcludeExtraView(innerPosition);
        }
    }

    class FooterViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.loading_lay)
        RelativeLayout lay;
        @Bind(R.id.loading_text)
        TextView lT;
        @Bind(R.id.loading_img)
        ProgressBar loading;

        public FooterViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

    }
}
